package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.ListServic;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import com.atguigu.gmall.produck.client.ProductFeignClient;
import io.swagger.annotations.Api;
import org.elasticsearch.client.ElasticsearchClient;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: TODO
 * @Author: Mr.Cao
 * @DateTime: 2021/10/13 18:38
 * BMW F750GS
 **/
@RestController
@RequestMapping("api/list")
@Api(tags = "搜索微服务")
public class ListApiController {

    /**
     * Elasticsearch官方提供的
     * Elasticsearch高级别客户端
     * */
    @Autowired
    private RestHighLevelClient restHighLevelClient;

    /**
     * spring官方提供的Elasticsearch高级别客户端
     * */
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    private ListServic listService;

    /**
     * 创建Elasticsearch索引库
     * */
    @GetMapping("createElasticsearchIndex")
    private void createElasticsearchIndex(){
        //创建索引库
        elasticsearchRestTemplate.createIndex(Goods.class);
        //创建Mapping
        elasticsearchRestTemplate.putMapping(Goods.class);
    }


    /**
     * sku 商品上架
     * */
    @PostMapping("inner/onSale/{skuId}")
    public Result onSale(@PathVariable long skuId){
        listService.onSale(skuId);
        return Result.ok();
    }

    @PostMapping("inner/onSale/noJuc/{skuId}")
    public Result onSaleNoJuc(@PathVariable long skuId){
        listService.onSaleNoJuc(skuId);
        return Result.ok();
    }
    /**
     * sku 商品下架
     * */
    @PostMapping("inner/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable long skuId){
        listService.cancelSale(skuId);
        return Result.ok();
    }
    /**
     * sku商品 增加热度值
     * */
    @PutMapping("inner/incrHostScore/{skuId}/{score}")
    public void incrHostScore(@PathVariable long skuId,@PathVariable long score){
        listService.incrHostScore(skuId,score);
    }
    //开始搜索
    @PostMapping("/inner/search")
    public SearchResponseVo search(@RequestBody SearchParam searchParam){
        return listService.search(searchParam);
    }
}
